package com.test.springboot.springboot3securitydemo1.controller;

import java.util.Collection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/9/20-12:13
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Controller
public class LoginController {
  private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);

  @RequestMapping("/login")
  public String login() {
    LOGGER.info("login");
    return "login";
  }

  @RequestMapping("/index")
  public String index() {
    LOGGER.info("index");
    return "home";
  }

  @ResponseBody
  @RequestMapping("/hello")
  public String hello() {
    var auth = SecurityContextHolder.getContext().getAuthentication();
    final String name = auth.getName();
    final Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();
    LOGGER.info("name = {}", name);
    LOGGER.info("authorities = {}", authorities);
    return "Hello spring security";
  }
}
